package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.Overall;
import com.ruoyi.system.mapper.OverallMapper;
import com.ruoyi.system.service.IOverallService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hwh
 * @ClassName: $
 * @Description: $
 * @Date $
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OverallServiceImpl extends ServiceImpl<OverallMapper, Overall> implements IOverallService {

    @Resource
    private OverallMapper overallMapper;

    @Override
    public List<Overall> selectOverallList(Overall overall) {
        return overallMapper.selectOverallList(overall);
    }

    @Override
    public boolean addOverall(Overall overall) {
        if (overallMapper.selectOverallList(overall).size() > 0) {
            throw new RuntimeException("该统筹区名称已经存在,请重新输入");
        }
        return overallMapper.addOverall(overall);
    }

    @Override
    public boolean updateOverall(Overall overall) {
        Overall overallNull = new Overall();
        //查询出所有的名称
        List<String> overallName = overallMapper.selectOverallList(overallNull).stream().map(Overall::getOverall).collect(Collectors.toList());
        Overall overallById = overallMapper.selectOverall(overall.getOverallId());
        if (overallName.contains(overall.getOverall()) && !overallById.getOverall().equals(overall.getOverall())) {
            throw new RuntimeException("该统筹区名称已经存在,请重新输入");
        }
        return overallMapper.updateOverall(overall);
    }

    @Override
    public boolean deleteOverall(Integer overallId) {
        return overallMapper.deleteOverall(overallId);
    }
}
